﻿using Engine;
using Engine.Graphics;
using Game;

namespace Mekiasm
{
    public class FlyBackPack : ClothingItem,Interface.IChargeAbleItem
    {
        public Common.SoundItem SoundItem = new Common.SoundItem("jetpack");

        public static int Index;

        bool flag = false;

        FireworksTrailParticleSystem flyParticle = new FireworksTrailParticleSystem();

        public ComponentEnergyMachine.FuelType ChargeFuelType => ComponentEnergyMachine.FuelType.Gas;

        public long MaxAmount => GetMaxPower();

        public FlyBackPack() : base( "喷气背包")
        {
            Clothing.ArmorProtection = 0.6f;

        }

        public override void ClothingEquipedUpdate(int value, ComponentPlayer componentPlayer)
        {
            flyParticle.IsStopped = !flag;
            var tran = componentPlayer.ComponentClothing.m_componentOuterClothingModel.GetBoneTransform(0);
            if (tran.HasValue)
            {
                flyParticle.Position = tran.Value.Translation - new Vector3(tran.Value.Forward.X * 0.2f, tran.Value.Forward.Y - 0.8f, tran.Value.Forward.Z * 0.2f);
            }
            SaveItemSystem.GetBlockData(value, out Common.SaveItem toolSaveItem);
            if (toolSaveItem != null && toolSaveItem.fuelAmount > 0)
            {
                if (componentPlayer.ComponentInput.PlayerInput.Jump)
                {
                    flag = !flag;
                    if (flag) DisplayMsg(componentPlayer,"起飞");
                    else
                    {
                        SoundItem.Pause();
                        DisplayMsg(componentPlayer,"降落");
                    }
                }
                if (flag)
                {
                    if (ILibrary.SubsystemTime.PeriodicGameTimeEvent(1.0, 0.0))
                    {
                        SaveItemSystem.ChangePower(componentPlayer.ComponentMiner.Inventory, componentPlayer.ComponentMiner.Inventory.ActiveSlotIndex, -10);
                    }
                    if (componentPlayer.ComponentInput.PlayerInput.Move.Z > 0)
                    {
                        componentPlayer.ComponentBody.Velocity = new Vector3(
                            componentPlayer.GameWidget.ActiveCamera.ViewDirection.X * 5f,
                            componentPlayer.GameWidget.ActiveCamera.ViewDirection.Y < 0f ? 0f : 3f,
                            componentPlayer.GameWidget.ActiveCamera.ViewDirection.Z * 5f
                            );
                    }
                    else
                    {
                        componentPlayer.ComponentBody.Velocity = new Vector3(
                            0f,
                            componentPlayer.GameWidget.ActiveCamera.ViewDirection.Y < 0f ? 0f : 3f,
                            0f
                            );
                    }
                    if (componentPlayer.GameWidget.ActiveCamera.ViewDirection.Y <= 0)
                    {//悬停
                        if (ILibrary.SubsystemTime.PeriodicGameTimeEvent(0.1, 0.02)) SoundItem.Play();
                        if (ILibrary.SubsystemTime.PeriodicGameTimeEvent(0.1, 0.08)) SoundItem.Pause();
                    }
                    else
                    {//上升
                        SoundItem.Volume = 0.7f;
                        SoundItem.Play();
                    }
                }
            }
            else
            {
                SoundItem.Pause();
                if (flag)
                {

                    flag = false;
                    DisplayMsg(componentPlayer,"降落");
                }
            }
            if (toolSaveItem == null || toolSaveItem.Power == 0)
            {
                Clothing.ArmorProtection = 0f;
            }
            else
            {
                Clothing.ArmorProtection = 0.6f;
            }
            if (flag && !ILibrary.SubsystemParticles.ContainsParticleSystem(flyParticle))
            {

                ILibrary.SubsystemParticles.AddParticleSystem(flyParticle);
            }
            else  if(!flag && ILibrary.SubsystemParticles.ContainsParticleSystem(flyParticle))
            {
                ILibrary.SubsystemParticles.RemoveParticleSystem(flyParticle);
            }

        }


        public override int GetMaxPower()
        {
            return 10240;
        }

        public override bool IsCrushWhenNoPower()
        {
            return false;
        }
        public override bool IsClothing()
        {
            return true;
        }

        public override ClothingSlot GetClothingSlot()
        {
            return ClothingSlot.Torso;
        }

        public override Texture2D GetTexture()
        {
            return ContentManager.Get<Texture2D>("Mekiasm/Textures/Clothing/飞行背包");
        }

        public override bool IsOuter()
        {
            return true;
        }


        public override float GetBlockHealth(int value)
        {
            SaveItemSystem.GetBlockData(value, out var d);
            if (d == null) return -1f;
            else return GetCurrentAmount(value) / (float)GetMaxPower();
        }


        public long GetCurrentAmount(int value)
        {
            SaveItemSystem.GetBlockData(value, out var d);
            if (d == null) return 0;
            else return d.fuelAmount;
        }

        public int SetCurrentAmount(int value, int fuelValue, long varAmount)
        {
            return SaveItemSystem.ChangePower(value, varAmount, fuelValue);
        }

        public int GetChargeFuelValue(int value)
        {
            SaveItemSystem.GetBlockData(value, out var d);
            if (d == null) return 0;
            else return d.fuelValue;
        }

        public bool CanAcceptFuel(int value)
        {
            if (value == Hydrogen.Index) return true;
            return false;
        }
    }
}
